package com.bytz.modules.cms.pay.controller;

import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.order.model.PayNotifyResult;
import com.bytz.modules.cms.pay.model.QueryPayIn;
import com.bytz.modules.cms.pay.model.StartPayIn;
import com.bytz.modules.cms.pay.service.IPayOnlineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * 在线支付
 * 创建时间	2022年10月28日
 */
@Slf4j
@Api(tags ="线上支付")
@RestController
@RequestMapping(value = "payOnline")
public class PayController {
	@Autowired
	private IPayOnlineService payOnlineService;

	@ApiOperation(value = "发起支付页面")
	@RequestMapping(value = "startPayPage", method = RequestMethod.POST)
	public Result<String> startPay(@RequestBody @Valid StartPayIn startPayIn) {
		return Result.ok("成功", payOnlineService.startPayPage(startPayIn));
	}

	@ApiOperation(value = "前台通知")
	@RequestMapping(value = "payNotifyFront/{payWay}", method = RequestMethod.POST)
	public void payNotifyFront(@PathVariable("payWay") String payWay, HttpServletRequest request, HttpServletResponse response) throws Exception {
		payOnlineService.payFrontNotify(request, response, payWay);

		response.setContentType("text/html");
		response.getWriter().print("<script>window.close();</script>");
	}

	@ApiOperation(value = "后台通知")
	@RequestMapping(value = "payNotifyBack/{payWay}", method = RequestMethod.POST)
	public void payNotifyBack( @PathVariable("payWay") String payWay, HttpServletRequest request, HttpServletResponse response) throws IOException {
		log.info("银联接收后台通知开始");
		payOnlineService.payBackNotify(request, payWay);
	}

	@ApiOperation(value = "查询支付结果")
	@RequestMapping(value = "queryPayResult", method = RequestMethod.POST)
	public Result<?> queryPayResult(@RequestBody @Valid QueryPayIn queryPayIn) {
		return Result.ok(payOnlineService.queryPayResult(queryPayIn));
	}
}
